package com.shaoshaohuo.app.ui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.TimePickerView;
import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.adapter.SureOderListView;
import com.shaoshaohuo.app.adapter.SureOderShopAdapter;
import com.shaoshaohuo.app.entity.DeliverQueryENtity;
import com.shaoshaohuo.app.entity.FragmentNewCarEntity;
import com.shaoshaohuo.app.entity.MyCouponEntity;
import com.shaoshaohuo.app.entity.NewPickEntity;
import com.shaoshaohuo.app.entity.OrderShopInfoEntity;
import com.shaoshaohuo.app.entity.PickCommitOrderSuccess;
import com.shaoshaohuo.app.entity.SureOrederPayinformEntity;
import com.shaoshaohuo.app.net.ShopHttpConfig;
import com.shaoshaohuo.app.utils.Okhttputils;
import com.shaoshaohuo.app.utils.StringUtil;
import com.shaoshaohuo.app.utils.ToastUtil;

import java.io.IOException;
import java.io.Serializable;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;


public class SureOrderPayInformation extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {
    private String intent_data = "intentData";
    private int item_position;

    private int couponmoney = 0;
    private int numcoupon = 0;
//    private String gouwuche = "1";
    /**
     * tobar
     */
    private ImageView order_tobar_back;
    private TextView order_tobar_enter;
    private ImageView order_tobar_right;
    private LinearLayout order_layout_topbar_left;
    /**
     * 展示优惠券积分等信息
     */
    private List<String> list_data;
    private ListView sure_order_user_information;
    private SureOderListView sure_order_information_adapter;
    private List<MyCouponEntity.DataBean> dataBeen;
    /**
     * 展示购买商品列表
     */
    private ListView sure_order_shop_list;
    private SureOderShopAdapter sure_order_shop_adapter;
    private List<FragmentNewCarEntity.DataBean> sure_order_shop_data;
    private FragmentNewCarEntity fragmentNewCarEntity;

    private Button sure_order_commit;
    /**
     * 确认应付金额
     */
    private float totalMoney;
    private TextView sure_order_pay_money;//付款金额
    private TextView sure_order_total_price;//合计
    private String gotype = "0";//购物车或者订单详情
    /**
     * 管理守收货地址
     */
    private RelativeLayout sure_order_add_address_relative;
    private RelativeLayout sure_order_have_address;
    private TextView sure_order_name;
    private TextView sure_order_phone;
    private TextView sure_order_address;
    private String addressId = "";
    private MyBrocasrReciver myBrocasrReciver;
    private RelativeLayout sure_order_peisong_hint;
    private LinearLayout sure_order_shop_pick_linearLayout;
    private RelativeLayout sure_order_pick_into_relative;
    private String sorttype = "1";//采摘或者是普通商品
    private TextView sure_order_pick_into_xianshi_hour;
    private ImageView sure_order_pick_into_select_hour;
    private TimePickerView timer_select;
    private int childNum;
    private int yuanjiaNum;
    private int shareNum;
    private NewPickEntity top;
    private List<NewPickEntity> pick_data;
    private List<Integer> pickNum;
    private int pos = 0;
    private String couponid = "";

    public double towd(double d) {
        double f = d;
        BigDecimal b = new BigDecimal(f);
        double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return f1;
    }

    public SureOrderPayInformation newInstance(Intent intent, List<OrderShopInfoEntity.DataBean> list, int position) {
        SureOrderPayInformation sureOrderPayInformation = new SureOrderPayInformation();
        Bundle bundle = new Bundle();
        bundle.putSerializable(intent_data, (Serializable) list);
        bundle.putInt("position", position);
        intent.putExtras(bundle);
        return sureOrderPayInformation;
    }

    public void getIntentData() {
        Intent intent = getIntent();
        if (intent != null) {
            sorttype = intent.getStringExtra("sorttype");
            if (sorttype.equals("1")) {
                fragmentNewCarEntity = (FragmentNewCarEntity) intent.getExtras().get("fragmentNewCarEntity");
                gotype = intent.getStringExtra("gotype");
                if (gotype == null) {
                    gotype = "0";
                }
                sure_order_shop_data = fragmentNewCarEntity.getData();
                Log.e("sure_order_shop_data", sure_order_shop_data.size() + "");
            } else if (sorttype.equals("2")) {
                childNum = intent.getExtras().getInt("child");
                yuanjiaNum = intent.getExtras().getInt("yuanjia");
                shareNum = intent.getExtras().getInt("share");
                top = (NewPickEntity) intent.getSerializableExtra("newpick");
                Log.e("NewPickEntity", childNum + "==" + yuanjiaNum + shareNum + "==" + top.getData().getChild());
            }
            if (sorttype == null) {
                sorttype = "1";
            }

        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sure_order_pay_information);
        getIntentData();
        myBrocasrReciver = new MyBrocasrReciver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("finsh");
        registerReceiver(myBrocasrReciver, intentFilter);
        initView();
        if (sorttype.equals("2")) {
            initTimerSelect();
        }
        requestAddress();
        getData();
        calculate();
//        loadData();
        bindAdapter();
        initListener();
    }

    private void requestAddress() {
        if (sorttype.equals("1")) {
            sure_order_shop_pick_linearLayout.setVisibility(View.GONE);
            sure_order_pick_into_relative.setVisibility(View.GONE);
            sure_order_have_address.setVisibility(View.VISIBLE);
            sure_order_peisong_hint.setVisibility(View.VISIBLE);
            sure_order_add_address_relative.setVisibility(View.VISIBLE);
            ShopHttpConfig.Addresssearch(this, "1", new Okhttputils.OkutListener() {
                @Override
                public void error(Call call, IOException e) {

                }

                @Override
                public void error(Call call, int code) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            sure_order_have_address.setVisibility(View.GONE);
                            sure_order_add_address_relative.setVisibility(View.VISIBLE);
                        }
                    });

                }

                @Override
                public void suseff(Call call, Response response, Object o) {
                    final DeliverQueryENtity a = (DeliverQueryENtity) o;
                    final String province = a.getData().get(0).getProvince();
                    final String city = a.getData().get(0).getCity();
                    final String country = a.getData().get(0).getCounty();
                    final String details = a.getData().get(0).getDetails();
                    addressId = a.getData().get(0).getAddressid();
                    //直接复制
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            sure_order_have_address.setVisibility(View.VISIBLE);
                            sure_order_add_address_relative.setVisibility(View.GONE);
                            sure_order_name.setText(a.getData().get(0).getName());
                            sure_order_phone.setText(a.getData().get(0).getPhone());
                            sure_order_address.setText(province + city + country + details);
                        }
                    });


                }
            }, DeliverQueryENtity.class);
        } else if (sorttype.equals("2")) {
            sure_order_shop_pick_linearLayout.setVisibility(View.VISIBLE);
            sure_order_pick_into_relative.setVisibility(View.VISIBLE);
            sure_order_peisong_hint.setVisibility(View.GONE);
            sure_order_have_address.setVisibility(View.GONE);
            sure_order_add_address_relative.setVisibility(View.GONE);
            pick_data = new ArrayList<>();
            decidePickBuyBum();
        }
    }

    private void decidePickBuyBum() {
        pickNum = new ArrayList<>();
        if (childNum != 0) {
            pick_data.add(top);
            pickNum.add(1);
        }
        if (shareNum != 0) {
            pick_data.add(top);
            pickNum.add(2);
        }
        if (yuanjiaNum != 0) {
            pick_data.add(top);
            pickNum.add(3);
        }
        Log.e(" pickNum", pickNum.size() + "");
    }

    private void calculate() {
        if (sorttype.equals("1")) {
            getTotalMoney(fragmentNewCarEntity.getData());
        }
        if (sorttype.equals("2")) {
            getPickMoney();
        }
    }

    private float getPickMoney() {
        float pickMoney = 0;
        float childMoney = childNum * Float.valueOf(top.getData().getChild());
        float shareMoney = shareNum * Float.valueOf(top.getData().getTeam());
        float yuanjiaMoney = yuanjiaNum * Float.valueOf(top.getData().getAdult());
        pickMoney = childMoney + shareMoney + yuanjiaMoney;
        pickMoney = Float.valueOf(StringUtil.zeor(pickMoney + ""));
        sure_order_total_price.setText(pickMoney + "");
        sure_order_pay_money.setText(pickMoney + "");
        BigDecimal b = new BigDecimal(pickMoney);
        float f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
        return f1;
    }

    private float getTotalMoney(List<FragmentNewCarEntity.DataBean> data) {
        if (gotype.equals("1")) {
            String buy_num;//商品数量
            String fin_money;//商品价格
            int details;
            float shop_number;
            float shop_money;
            float shop_total_money; //单个商品总价
            totalMoney = 0;     //所有商品总价
            for (int i = 0; i < data.size(); i++) {
                //循环取得所有商品的钱数
                buy_num = data.get(i).getNum();
                details = data.get(i).getMbn_details();
                fin_money = data.get(i).getPrice();

                shop_number = Float.parseFloat(buy_num);

                shop_money = Float.parseFloat(fin_money);
                shop_total_money = shop_number * shop_money * details;
                totalMoney += shop_total_money;
//            Log.e("总钱数", v + "");
            }
//        SpannableString total_Money = new SpannableString("￥" + totalMoney);
//        AbsoluteSizeSpan span = new AbsoluteSizeSpan(30);
//        total_Money.setSpan(span, 1, 4, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
            Log.e("totalMoney前", totalMoney + "");
//            totalMoney = Float.valueOf(StringUtil.zeor(totalMoney + ""));
            Log.e("totalMoney后", totalMoney + "");
            BigDecimal b = new BigDecimal(totalMoney);
            float f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
            sure_order_pay_money.setText("￥" + (f1 - couponmoney));
//        SpannableString span_Price = new SpannableString("￥" + totalMoney);
//        AbsoluteSizeSpan spanPrice = new AbsoluteSizeSpan(30);
//        span_Price.setSpan(spanPrice, 1, 4, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
            sure_order_total_price.setText("￥" + (f1 - couponmoney));
            return f1 - couponmoney;
        }
        if (!gotype.equals("1")) {
            fragmentNewCarEntity.getData().get(0).setNum("1");
            String num = fragmentNewCarEntity.getData().get(0).getNum();
            String price = fragmentNewCarEntity.getData().get(0).getPrice();
            float a = Float.valueOf(num) > 0 ? Float.valueOf(num) : 1;
            float i = a * (Float.valueOf(price) * Float.valueOf(fragmentNewCarEntity.getData().get(0).getMbn_details()));
            Log.e("TAG", "getTotalMoney: " + i);
            i = Float.valueOf(StringUtil.zeor(i + ""));
//            Float aDouble = Float.valueOf(StringUtil.zeor(i + ""));
//            Log.e("aDouble", aDouble + "");

            BigDecimal b = new BigDecimal(i);
            float f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
            sure_order_total_price.setText(f1 - couponmoney + "");
            sure_order_pay_money.setText(f1 - couponmoney + "");
            return f1 - couponmoney;
        }
        return 0;
    }

    private void initListener() {
        sure_order_commit.setOnClickListener(this);
        sure_order_shop_list.setOnItemClickListener(this);
        order_tobar_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        sure_order_add_address_relative.setOnClickListener(this);
        sure_order_have_address.setOnClickListener(this);
        sure_order_pick_into_select_hour.setOnClickListener(this);
        sure_order_user_information.setOnItemClickListener(this);
    }

    private void bindAdapter() {
        sure_order_user_information.setAdapter(sure_order_information_adapter);
        sure_order_shop_list.setAdapter(sure_order_shop_adapter);
    }


    private void getData() {
        list_data.add("优惠券");
        list_data.add("积分");
        list_data.add("商品应付");
        list_data.add("运费");
        sure_order_shop_adapter = new SureOderShopAdapter(this, pickNum, sure_order_shop_data, sorttype, pick_data, childNum, shareNum, yuanjiaNum);
        if (sorttype.equals("1")) {
            loadData();
            if (!gotype.equals("1")) {
                fragmentNewCarEntity.getData().get(0).setNum("1");
                String num = fragmentNewCarEntity.getData().get(0).getNum();
                String price = fragmentNewCarEntity.getData().get(0).getPrice();
                float a = Float.valueOf(num) > 0 ? Float.valueOf(num) : 1;
                float i = a * (Float.valueOf(price) * Float.valueOf(fragmentNewCarEntity.getData().get(0).getMbn_details()));
//                i = float.valueOf(StringUtil.zeor(i + ""));
                BigDecimal b = new BigDecimal(i);
                float f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
                sure_order_information_adapter = new SureOderListView(this, list_data, f1, couponmoney, numcoupon);
            } else {
                Log.e("Tag", getTotalMoney(sure_order_shop_data) + "");
                sure_order_information_adapter = new SureOderListView(this, list_data, getTotalMoney(sure_order_shop_data), couponmoney, numcoupon);
            }
        } else {
//            String child1 = pick_data.get(0).getData().getChild();
            float child = Float.valueOf(pick_data.get(0).getData().getChild());
            float childMoney = childNum * child;
            float share = Float.valueOf(pick_data.get(0).getData().getTeam());
            float sharemoney = shareNum * share;
            float adult = Float.valueOf(pick_data.get(0).getData().getAdult());
            float adultmoney = yuanjiaNum * adult;
            float allmoney = adultmoney + childMoney + sharemoney;
            float i = allmoney;
            BigDecimal b = new BigDecimal(i);
            float f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
            sure_order_information_adapter = new SureOderListView(this, list_data, f1, couponmoney, numcoupon);
        }

    }

    private void initView() {
        /**
         * tobar
         * */
        order_tobar_back = (ImageView) findViewById(R.id.imageview_topbar_left_backImage);
        order_tobar_back.setVisibility(View.VISIBLE);
        order_tobar_back.setImageResource(R.mipmap.return_key);
        order_tobar_enter = (TextView) findViewById(R.id.toolbar_topname);
        order_tobar_enter.setText("确认订单");
        order_tobar_right = (ImageView) findViewById(R.id.imageview_message);
        order_tobar_right.setVisibility(View.GONE);
        order_layout_topbar_left = (LinearLayout) findViewById(R.id.layout_topbar_left);
        order_layout_topbar_left.setVisibility(View.VISIBLE);
        list_data = new ArrayList<>();
        sure_order_user_information = (ListView) findViewById(R.id.sure_order_user_information);
        sure_order_commit = (Button) findViewById(R.id.sure_order_commit);
        /**
         * 展示购买商品列表
         */
        sure_order_shop_list = (ListView) findViewById(R.id.sure_order_shop_list);
        dataBeen = new ArrayList<>();
        /**
         * 应付金额
         * */
        sure_order_pay_money = (TextView) findViewById(R.id.sure_order_pay_money);
        sure_order_total_price = (TextView) findViewById(R.id.sure_order_total_price);
        /**
         * 管理地址
         * */
        sure_order_add_address_relative = (RelativeLayout) findViewById(R.id.sure_order_add_address_relative);
        sure_order_have_address = (RelativeLayout) findViewById(R.id.sure_order_have_address);
        sure_order_name = (TextView) findViewById(R.id.sure_order_name);
        sure_order_phone = (TextView) findViewById(R.id.sure_order_phone);
        sure_order_address = (TextView) findViewById(R.id.sure_order_address);
        sure_order_peisong_hint = (RelativeLayout) findViewById(R.id.sure_order_peisong_hint);
        //采摘
        sure_order_shop_pick_linearLayout = (LinearLayout) findViewById(R.id.sure_order_shop_pick_linearLayout);
        sure_order_pick_into_relative = (RelativeLayout) findViewById(R.id.sure_order_pick_into_relative);
        sure_order_pick_into_select_hour = (ImageView) findViewById(R.id.sure_order_pick_into_select_hour);
        sure_order_pick_into_xianshi_hour = (TextView) findViewById(R.id.sure_order_pick_into_xianshi_hour);
        dataBeen = new ArrayList<>();
    }

    private void loadData() {
        ShopHttpConfig.canUseCoupon(getApplicationContext(), "1", getTotalMoney(sure_order_shop_data) + "", new Okhttputils.OkutListener() {
            @Override
            public void error(Call call, IOException e) {

            }

            @Override
            public void error(Call call, int code) {

            }

            @Override
            public void suseff(Call call, Response response, Object o) {
                MyCouponEntity myCouponEntity = (MyCouponEntity) o;
                dataBeen = myCouponEntity.getData();
                if (dataBeen.size() == 0) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
//                            coupon_no_data_relative.setVisibility(View.VISIBLE);
//                            couponList.setVisibility(View.GONE);
//                            Log.e("TAG", "run: " + coupon_listView_data.size());
                            numcoupon = dataBeen.size();
                            sure_order_information_adapter.refreshData(couponmoney, dataBeen.size());
                        }
                    });

                }
                if (dataBeen.size() != 0) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
//                            coupon_no_data_relative.setVisibility(View.GONE);
//                            couponList.setVisibility(View.VISIBLE);
//                            couponAdapter.refreshData(dataBeen);
//                            Log.e("TAGAAA", "run: " + coupon_listView_data.size() + mParam1);
                            numcoupon = dataBeen.size();
                            sure_order_information_adapter.refreshData(couponmoney, dataBeen.size());
                        }
                    });
                }

            }
        }, MyCouponEntity.class);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 110 && resultCode == 110) {
            Log.e("无地址", requestCode + "");
            requestAddress();
        }
        if (requestCode == 119 && resultCode == 119) {
            if (data.getExtras() != null) {
                DeliverQueryENtity.DataBean dataBean = (DeliverQueryENtity.DataBean) data.getExtras().get("back_data");
                Log.e("返回的数据", dataBean.getName());
                sure_order_name.setText(dataBean.getName());
                sure_order_phone.setText(dataBean.getPhone());
                addressId = dataBean.getAddressid();
                sure_order_address.setText(dataBean.getProvince() + dataBean.getCity() + dataBean.getCounty() + dataBean.getDetails());
            } else {
                requestAddress();
            }

        }
        if (requestCode == 120 && resultCode == 120) {
            if (data.getExtras() != null) {
//                couponmoney = (int) data.getExtras().get("money");
                String money1 = data.getExtras().getString("money");
                couponmoney = Integer.valueOf(money1);
                getTotalMoney(sure_order_shop_data);
                pos = data.getExtras().getInt("position");
                sure_order_information_adapter.refreshData(couponmoney, numcoupon);
            }
        }
        if (requestCode == 0x100 && resultCode == 0x0001) {
            if (gotype.equals("1")) {
                Intent intent = new Intent(this, ShopAllOrderActivity.class);
                intent.putExtra("intent_data", "全部");
                intent.putExtra("intent_position", 0);
                startActivity(intent);
//                ToastUtil.toast("跳轉");
                finish();
            } else {
                finish();
            }

        }
    }

    @Override
    public void onClick(View v) {
        final Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.sure_order_commit:
                if (pos != 0 || couponmoney != 0) {
                    couponid = dataBeen.get(pos).getId();
                }
                Log.e("TAGQ", "onClick: " + couponid + "==");
                if (sorttype.equals("1")) {
                    String orderids = "";
                    int allmoney = 0;
                    if (gotype.equals("1")) {
                        for (int i = 0; i < sure_order_shop_data.size(); i++) {
                            FragmentNewCarEntity.DataBean dataBean = sure_order_shop_data.get(i);
                            String orderid = dataBean.getOrderid();
                            if (orderids.length() == 0) {
                                orderids += orderid;
                            } else {
                                orderids += "_" + orderid;
                            }
                            String num = dataBean.getNum();
                            Integer innum = Integer.valueOf(num);
                            String price = dataBean.getPrice();
                            float inprice = Float.valueOf(price);
                            allmoney += innum * inprice;
                        }
                        String catid = sure_order_shop_data.get(0).getCatid();
                        Log.e("TAG", "onClick: addressId" + addressId + "orderids" + orderids);
                        if (addressId.length() > 0) {

                            ShopHttpConfig.App_Orederadd(this, orderids, null, null, addressId, couponid, "", getTotalMoney(fragmentNewCarEntity.getData()) + couponmoney + "", getTotalMoney(fragmentNewCarEntity.getData()) + "", "0.00", "1", new Okhttputils.OkutListener() {
                                @Override
                                public void error(Call call, IOException e) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            ToastUtil.toast("订单创建失败");
                                        }
                                    });
                                }

                                @Override
                                public void error(Call call, int code) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            ToastUtil.toast("订单创建失败");
                                        }
                                    });
                                }

                                @Override
                                public void suseff(Call call, Response response, Object o) {
                                    SureOrederPayinformEntity sureOrederPayinformEntity = (SureOrederPayinformEntity) o;
                                    Log.e("TAG", "suseff: " + sureOrederPayinformEntity.getData().getOrderid());
                                    String orderid = sureOrederPayinformEntity.getData().getOrderid();
                                    String fin_money = sureOrederPayinformEntity.getData().getFin_money();
                                    Intent intent1 = new Intent();
                                    Bundle bundle = new Bundle();
                                    bundle.putString("orderid", orderid);
                                    bundle.putString("fin_money", fin_money);
                                    bundle.putSerializable("shop_data", (Serializable) sure_order_shop_data);
                                    intent1.putExtras(bundle);
                                    intent1.setClass(SureOrderPayInformation.this, CommitPayOrder.class);
                                    startActivityForResult(intent1, 0x100);
                                }
                            }, SureOrederPayinformEntity.class);
                        } else {
                            ToastUtil.toast("请选择收货地址");
                        }
                    } else {
                        if (addressId.length() > 0) {
                            Log.e("gotype", gotype);
                            fragmentNewCarEntity.getData().get(0).setNum("1");
                            String num = fragmentNewCarEntity.getData().get(0).getNum();
                            String price = fragmentNewCarEntity.getData().get(0).getPrice();
                            float a = Float.valueOf(num) > 0 ? Float.valueOf(num) : 1;
                            float i = a * (Float.valueOf(price) * Float.valueOf(fragmentNewCarEntity.getData().get(0).getMbn_details()));
                            Log.e("TAG", "onClick: sadsadasssssssssssssss" + fragmentNewCarEntity.getData().get(0).getCatid());
//                            i = Float.valueOf(StringUtil.zeor(i + ""));
                            if (fragmentNewCarEntity.getData().get(0).getNum().equals("0")) {
                                fragmentNewCarEntity.getData().get(0).setNum("1");
                            }
                            BigDecimal b = new BigDecimal(i);
                            float f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
                            ShopHttpConfig.App_Orederadd(this, null, fragmentNewCarEntity.getData().get(0).getCatid(), fragmentNewCarEntity.getData().get(0).getNum(), addressId, couponid, "", f1 + "", f1 - couponmoney + "", "0.00", "2", new Okhttputils.OkutListener() {
                                @Override
                                public void error(Call call, IOException e) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            ToastUtil.toast("订单创建失败");
                                        }
                                    });
                                }

                                @Override
                                public void error(Call call, int code) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {

                                            ToastUtil.toast("订单创建失败");
                                        }
                                    });
                                }

                                @Override
                                public void suseff(Call call, Response response, Object o) {
                                    SureOrederPayinformEntity sureOrederPayinformEntity = (SureOrederPayinformEntity) o;
                                    Log.e("TAG", "suseff: " + sureOrederPayinformEntity.getData().getOrderid());
                                    String orderid = sureOrederPayinformEntity.getData().getOrderid();
                                    String fin_money = sureOrederPayinformEntity.getData().getFin_money();
                                    Intent intent1 = new Intent();
                                    Bundle bundle = new Bundle();
                                    bundle.putString("orderid", orderid);
                                    bundle.putString("fin_money", fin_money);
                                    bundle.putSerializable("shop_data", (Serializable) sure_order_shop_data);
                                    intent1.putExtras(bundle);
                                    intent1.setClass(SureOrderPayInformation.this, CommitPayOrder.class);
                                    startActivityForResult(intent1, 0x100);
                                }
                            }, SureOrederPayinformEntity.class);


                        } else ToastUtil.toast("请选择收货地址");
                    }
                }
                if (sorttype.equals("2")) {
                    getPickData();
                }
                break;
            case R.id.sure_order_add_address_relative:
                intent.setClass(this, DeliveryaddressActivity.class);
                intent.putExtra("type", "1");
                intent.putExtra("have_no_address", "110");
                startActivityForResult(intent, 110);
                break;
            case R.id.sure_order_have_address:
                intent.setClass(this, DeliveryaddressActivity.class);
                intent.putExtra("type", "1");
                intent.putExtra("have_no_address", "119");
                startActivityForResult(intent, 119);
                break;
            case R.id.sure_order_pick_into_select_hour:
                timer_select.show(sure_order_pick_into_xianshi_hour);
                break;
        }
    }

    private void getPickData() {
//        float child_money = childNum * Float.valueOf(pick_data.get(0).getData().getChild());
//        float share_money = shareNum * Float.valueOf(pick_data.get(0).getData().getTeam());
//        float yuanjia_money = yuanjiaNum * Float.valueOf(pick_data.get(0).getData().getAdult());
//        String money = yuanjia_money + share_money + child_money + "";
        String pickMoney = getPickMoney() + "";
        String hour = sure_order_pick_into_xianshi_hour.getText().toString().trim();
        if (hour.equals("(请选择)")) {
            Toast.makeText(this, "请选择入园时间", Toast.LENGTH_SHORT).show();
        } else {
            ShopHttpConfig.commitPickOrder(getApplicationContext(), pick_data.get(0).getData().getCatid(), "", "", pickMoney, pickMoney, hour, yuanjiaNum + "", childNum + "", shareNum + "", new Okhttputils.OkutListener() {
                @Override
                public void error(Call call, IOException e) {

                }

                @Override
                public void error(Call call, final int code) {
                    Log.e("提交", "失败");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            decideOverplusType(code);
                        }
                    });

                }

                @Override
                public void suseff(Call call, Response response, final Object o) {
                    Log.e("提交", "成功");
                    final PickCommitOrderSuccess pickCommitOrderSuccess = (PickCommitOrderSuccess) o;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Intent intent = new Intent();
                            String orderid = pickCommitOrderSuccess.getData().getOrderid();
                            String fin_money = pickCommitOrderSuccess.getData().getFin_money();
                            intent.setClass(getApplicationContext(), CommitPayOrder.class);
                            Log.e("支付跳转", orderid + fin_money);
                            Bundle bundle = new Bundle();
                            bundle.putString("orderid", orderid);
                            bundle.putString("fin_money", fin_money);
                            intent.putExtras(bundle);
                            startActivity(intent);
                        }
                    });
                }
            }, PickCommitOrderSuccess.class);
        }
    }

    private void decideOverplusType(int code) {
        if (code == 389) {
            Toast.makeText(this, "非特价票已售空", Toast.LENGTH_SHORT).show();
        } else if (code == 390) {
            Toast.makeText(this, "特价票已售空", Toast.LENGTH_SHORT).show();
        } else if (code == 391) {
            Toast.makeText(this, "非特价票不足", Toast.LENGTH_SHORT).show();
        } else if (code == 392) {
            Toast.makeText(this, "特价票不足", Toast.LENGTH_SHORT).show();
        }
    }
//    private void getPickData() {
//        float child_money = childNum * Float.valueOf(pick_data.get(0).getData().getChild());
//        float share_money = shareNum * Float.valueOf(pick_data.get(0).getData().getTeam());
//        float yuanjia_money = yuanjiaNum * Float.valueOf(pick_data.get(0).getData().getAdult());
//        String money = yuanjia_money + share_money + child_money + "";
//        String hour = sure_order_pick_into_xianshi_hour.getText().toString().trim();
//        ShopHttpConfig.commitPickOrder(getApplicationContext(), pick_data.get(0).getData().getCatid(), "", "", money, money, hour, yuanjiaNum + "", childNum + "", shareNum + "", new Okhttputils.OkutListener() {
//            @Override
//            public void error(Call call, IOException e) {
//
//            }
//
//            @Override
//            public void error(Call call, int code) {
//                Log.e("提交", "失败");
//            }
//
//            @Override
//            public void suseff(Call call, Response response, Object o) {
//                Log.e("提交", "成功");
//                final PickCommitOrderSuccess pickCommitOrderSuccess = (PickCommitOrderSuccess) o;
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        Intent intent = new Intent();
//                        String orderid = pickCommitOrderSuccess.getData().getOrderid();
//                        String fin_money = pickCommitOrderSuccess.getData().getFin_money();
//                        Bundle bundle = new Bundle();
//                        bundle.putString("orderid", orderid);
//                        bundle.putString("fin_money", fin_money);
//                        intent.setClass(getApplicationContext(), CommitPayOrder.class);
//                        startActivity(intent);
//                    }
//                });
//            }
//        }, PickCommitOrderSuccess.class);
//    }

    private String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    private void initTimerSelect() {
        String openTimeEnd = top.getData().getOpenTimeEnd();
        String openTimeSta = top.getData().getOpenTimeSta();
        Calendar selectedDate = Calendar.getInstance();
        Calendar year = Calendar.getInstance();
        int i = year.get(Calendar.YEAR);
        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        int start_year = 0;
        int start_month = 0;
        int start_day = 0;
        int end_year = 0;
        int end_month = 0;
        int end_day = 0;

        try {
            Date end = dateFormat.parse(openTimeEnd);
            Date start = dateFormat.parse(openTimeSta);
            startDate.setTime(start);
            endDate.setTime(end);
            start_year = startDate.get(Calendar.YEAR);
            start_month = startDate.get(Calendar.MONTH);
            start_day = startDate.get(Calendar.DAY_OF_MONTH);
            end_year = endDate.get(Calendar.YEAR);
            end_month = endDate.get(Calendar.MONTH);
            end_day = endDate.get(Calendar.DAY_OF_MONTH);
        } catch (ParseException e) {
            e.printStackTrace();
        }
//        Log.e("", start_day + "==" + start_month + start_year + "---" + end_day + end_month + end_year);
        startDate.set(start_year, start_month, start_day);
        endDate.set(end_year, end_month, end_day);
        timer_select = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                TextView textView = (TextView) v;
                textView.setText(getTime(date));
            }
        }).setType(new boolean[]{true, true, true, false, false, false})
                .setLabel("年", "月", "日", "", "", "")
                .isCenterLabel(false)
                .setDividerColor(Color.DKGRAY)
                .setContentSize(21)
                .setRangDate(startDate, endDate)
                .setBackgroundId(0x00FFFFFF) //设置外部遮罩颜色
                .setDecorView(null)
                .build();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myBrocasrReciver);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (numcoupon == 0) {
            ToastUtil.toast("你还没有优惠券");
        } else {
            Intent intent = new Intent();
//        switch (view.getId()) {
//            case R.id.sure_order_user_information:
            if (position == 0) {
                intent.setClass(this, CanUseCouponActivity.class);
                intent.putExtra("totalmoney", getTotalMoney(sure_order_shop_data) + "");
                Bundle bundle = new Bundle();
                bundle.putSerializable("coupondata", (Serializable) dataBeen);
                intent.putExtras(bundle);
                startActivityForResult(intent, 120);
            }
        }
//                break;
//        }
//        Intent intent = new Intent();
//        intent.setClass(this, ItemOrderDetails.class);
//        startActivity(intent);
    }

//    @Override
//    public void refreshListner() {
//        getTotalMoney();
//    }
//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if (requestCode == 110 && resultCode == 110) {
//            requestAddress();
//        }
//        if (requestCode == 119 && resultCode == 119) {
//            DeliverQueryENtity.DataBean dataBean = (DeliverQueryENtity.DataBean) data.getExtras().get("back_data");
//            sure_order_name.setText(dataBean.getName());
//            sure_order_phone.setText(dataBean.getPhone());
//            addressId = dataBean.getAddressid();
//            sure_order_address.setText(dataBean.getProvince() + dataBean.getCity() + dataBean.getCounty() + dataBean.getDetails());
//        }
//    }

    public class MyBrocasrReciver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals("finsh")) {
                finish();
            }
        }
    }


}
